<template>
  <div
    class="carouselitem-container"
    ref="carouselitem"
    @mousemove="handleMousemove"
    @mouseleave="handleMouseLeave"
  >
    <div class="carousel-img" :style="imagePosition" ref="image">
      <ImageLoader
        @load="this.showWord"
        :src="carousel.bigImg"
        :placeholder="carousel.midImg"
      />
    </div>
    <div class="title" ref="titleDom">{{ carousel.title }}</div>
    <div class="description" ref="desc">{{ carousel.description }}</div>
  </div>
</template>

<script>
import ImageLoader from "@/components/ImageLoader";
export default {
  props: ["carousel"],
  data() {
    return {
      titleWidth: 0,
      descWidth: 0,
      containerSize: null,
      imageSize: null,
      mouseX: 0,
      mouseY: 0,
    };
  },
  components: {
    ImageLoader,
  },
  mounted() {
    this.titleWidth = this.$refs.titleDom.clientWidth;
    this.descWidth = this.$refs.desc.clientWidth;
    this.getSize();
    this.mouseX = this.center.x;
    this.mouseY = this.center.y;
    window.addEventListener("resize", this.getSize);
  },
  destoryed() {
    window.removeEventListener("resize", this.getSize);
  },
  computed: {
    imagePosition() {
      if (!this.containerSize || !this.imageSize) {
        return;
      }
      const extraWidth = this.containerSize.width - this.imageSize.width;
      const extraHeight = this.containerSize.height - this.imageSize.height;

      const left = (extraWidth / this.containerSize.width) * this.mouseX;
      const top = (extraHeight / this.containerSize.height) * this.mouseY;
      return {
        transform: `translate(${left}px, ${top}px)`,
      };
    },
    center() {
      return {
        x: this.containerSize.width / 2,
        y: this.containerSize.height / 2,
      };
    },
  },
  methods: {
    showWord() {
      this.$refs.titleDom.style.opacity = 1;
      this.$refs.titleDom.style.width = 0;
      this.$refs.titleDom.clientWidth;
      this.$refs.titleDom.style.transition = "all 1s";
      this.$refs.titleDom.style.width = this.titleWidth + "px";

      this.$refs.desc.style.opacity = 1;
      this.$refs.desc.style.width = 0;
      this.$refs.desc.clientWidth;
      this.$refs.desc.style.transition = "all 1s 1s";
      this.$refs.desc.style.width = this.descWidth + "px";
    },
    getSize() {
      if (!this.$refs.carouselitem || !this.$refs.image) {
        return;
      }
      this.containerSize = {
        width: this.$refs.carouselitem.clientWidth,
        height: this.$refs.carouselitem.clientHeight,
      };
      // console.log(this.containerSize)
      this.imageSize = {
        width: this.$refs.image.clientWidth,
        height: this.$refs.image.clientHeight,
      };
    },
    handleMousemove(e) {
      const rect = this.$refs.carouselitem.getBoundingClientRect();
      this.mouseX = e.clientX - rect.left;
      this.mouseY = e.clientY - rect.top;

      // console.log(rect, this.mouseX, this.mouseY)
      // this.clientX = this.e.clientX -
      // console.log(e.clientX, e.clientY)
    },
    handleMouseLeave(e) {
      this.mouseX = this.center.x;
      this.mouseY = this.center.y;
    },
  },
};
</script>

<style lang="less" scoped>
.carouselitem-container {
  width: 100%;
  height: 100%;
  position: relative;
  color: #fff;
  overflow: hidden;
  .title,
  .description {
    opacity: 0;
    white-space: nowrap;
    overflow: hidden;
    position: absolute;
    left: 20px;
    letter-spacing: 3px;
    text-shadow: 1px 0px 0px rgba(0, 0, 0, 0.6), -1px 0px 0px rgba(0, 0, 0, 0.6),
      0px 1px 0px rgba(0, 0, 0, 0.6), 0px -1px 0px rgba(0, 0, 0, 0.6);
  }
  .title {
    top: calc(50% - 20px);
    font-size: 2em;
  }
  .description {
    top: calc(50% + 40px);
    font-size: 1.2em;
  }
}
.carousel-img {
  width: 110%;
  height: 110%;
  position: absolute;
  left: 0;
  top: 0;
  // z-index: -1;
  transition: all 0.2s;
}
</style>